package com.j283_5.wisdom_park.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.j283_5.wisdom_park.common.result.ResultData;
import com.j283_5.wisdom_park.mapper.ProjectMapper;
import com.j283_5.wisdom_park.pojo.Project;
import com.j283_5.wisdom_park.pojo.Roleinfo;
import com.j283_5.wisdom_park.pojo.Visitors;
import com.j283_5.wisdom_park.service.ProjectService;
import com.j283_5.wisdom_park.vo.confcenter_management.ProjectVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author laoda
 * @since 2022-04-28
 */
@Service
public class ProjectServiceImpl extends ServiceImpl<ProjectMapper, Project> implements ProjectService {
    @Resource
    private ProjectMapper projectMapper;


    @Override
    public ResultData getProjectPage(Page<Project> page,Project project) {
        if(page==null){
            page = new Page<>(1,10);
        }
        QueryWrapper<Project> projectQueryWrapper=new QueryWrapper<>();
        projectQueryWrapper.eq("pro_usstate",0);
        projectQueryWrapper.orderByDesc("pro_time");
        if (project.getProName()!=null){
            projectQueryWrapper.like("pro_name",project.getProName());
        }
        if (project.getProNum()!=null){
            projectQueryWrapper.like("pro_num",project.getProNum());
        }
        if (project.getProState()!=null){
            projectQueryWrapper.eq("pro_state",project.getProState());
        }
        Page<Project> projectPage=projectMapper.selectPage(page,projectQueryWrapper);
        if (projectPage!=null){
            return ResultData.ok().data(projectPage);
        }else {
            return ResultData.error().message("查询失败!");
        }
    }

    @Override
    public ResultData addProjectInfo(ProjectVo projectVo) {
        Project project=new Project();
        BeanUtils.copyProperties(projectVo,project);
        int i=projectMapper.insert(project);
        if (i>0){
            return ResultData.ok().message("添加成功!");
        }
        return ResultData.ok().message("添加失败!");
    }

    @Override
    public ResultData deleteProjectInfo(ArrayList<Integer> proIdlist) {
        UpdateWrapper<Project> projectUpdateWrapper=new UpdateWrapper<>();
        projectUpdateWrapper.set("pro_usstate",1);
        projectUpdateWrapper.in("pro_id",proIdlist);
        int i=projectMapper.update(null,projectUpdateWrapper);
        if(i>0){
            return ResultData.ok().data("删除成功!");
        }
        return ResultData.ok().data("删除失败!");
    }

    @Override
    public ResultData updateProjectInfo(Project project) {
        UpdateWrapper<Project> projectUpdateWrapper=new UpdateWrapper<>();
        projectUpdateWrapper.eq("pro_id",project.getProId())
                .set("pro_name",project.getProName())
                .set("pro_num",project.getProNum())
                .set("pro_type",project.getProType())
                .set("pro_addr",project.getProAddr())
                .set("us_type",project.getUsType())
                .set("pro_starttime",project.getProStarttime())
                .set("pro_time",project.getProTime())
                .set("pro_state",project.getProState());
        int i = projectMapper.update(null,projectUpdateWrapper);
        if (i>0){
            return ResultData.ok().data("修改成功!");
        }else {
            return ResultData.ok().data("修改失败!");
        }
    }
}
